package com.lvmama.pangolin.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.entity.TicketCommodityGrade;
import com.lvmama.pangolin.entity.TicketCommodityGradeVO;
import com.lvmama.pangolin.enums.Company;
import com.lvmama.pangolin.mybatis.mapper.TicketCommodityGradeMapper;
import com.lvmama.pangolin.service.TicketCommodityGradeService;

@Service
public class TicketCommodityGradeServiceImpl implements TicketCommodityGradeService {

	@Autowired
	private TicketCommodityGradeMapper ticketCommodityGradeMapper;

	@Override
	@Transactional
	public int insert(TicketCommodityGrade ticketCommodityGrade) {
		int result = ticketCommodityGradeMapper.insert(ticketCommodityGrade);
		return result;
	}

	@Override
	public List<TicketCommodityGradeVO> query(TicketCommodityGrade ticketCommodityGrade) {
		// 默认查询驴妈妈产品
		if (StringUtils.isEmpty(ticketCommodityGrade.getCompanyCode())) {
			ticketCommodityGrade.setCompanyCode(Company.XC.getCode());
		}
		// 设置ticket_product_${tableSuffix}表后缀. eg.ticket_product_lmm
		ticketCommodityGrade.setTableSuffix(StringUtils.lowerCase(ticketCommodityGrade.getCompanyCode()));
		// 获取前一天的数据
		ticketCommodityGrade.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
		String monitorPlatform = Company.getNameByCode(ticketCommodityGrade.getCompanyCode());
		List<TicketCommodityGradeVO> resultList = ticketCommodityGradeMapper.query(ticketCommodityGrade);
		handleTicketCommodity(resultList, monitorPlatform);

		return resultList;
	}

	@Override
	public PageInfo<TicketCommodityGradeVO> queryByPage(TicketCommodityGrade ticketCommodityGrade, int pageNumber,
			int pageSize) {
		PageHelper.startPage(pageNumber, pageSize);

		// 默认查询驴妈妈产品
		if (StringUtils.isEmpty(ticketCommodityGrade.getCompanyCode())) {
			ticketCommodityGrade.setCompanyCode(Company.XC.getCode());
		}
		// 设置ticket_product_${tableSuffix}表后缀. eg.ticket_product_lmm
		ticketCommodityGrade.setTableSuffix(StringUtils.lowerCase(ticketCommodityGrade.getCompanyCode()));
		// 获取前一天的数据
		ticketCommodityGrade.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
		String monitorPlatform = Company.getNameByCode(ticketCommodityGrade.getCompanyCode());
		List<TicketCommodityGradeVO> resultList = ticketCommodityGradeMapper.query(ticketCommodityGrade);

		handleTicketCommodity(resultList, monitorPlatform);

		PageInfo<TicketCommodityGradeVO> pageInfo = new PageInfo<TicketCommodityGradeVO>(resultList);

		return pageInfo;
	}

	private void handleTicketCommodity(List<TicketCommodityGradeVO> list, String monitorPlatform) {
		for (TicketCommodityGradeVO tc : list) {
			tc.setSalePriceGrade(compareValues2(tc.getSalePrice(), tc.getLmmSalePrice()));
			tc.setLmmSalePriceGrade(compareValues2(tc.getLmmSalePrice(), tc.getSalePrice()));
			tc.setPreferentialGrade(compareValues1(tc.getPreferentialValue(), tc.getLmmPreferentialValue()));
			tc.setLmmPreferentialGrade(compareValues1(tc.getLmmPreferentialValue(), tc.getPreferentialValue()));
			tc.setBookTimeGrade(compareValues2(tc.getBookTimeValue(), tc.getLmmBookTimeValue()));
			tc.setLmmBookTimeGrade(compareValues2(tc.getLmmBookTimeValue(), tc.getBookTimeValue()));
			tc.setRefundCondGrade(compareValues1(tc.getRefundCondValue(), tc.getLmmRefundCondValue()));
			tc.setLmmRefundCondGrade(compareValues1(tc.getLmmRefundCondValue(), tc.getRefundCondValue()));
			tc.setMonitorPlatform(monitorPlatform);
		}
	}

	/**
	 * mianVal的值越大，得到的分数越高
	 * 
	 * @param mainVal
	 * @param subVal
	 * @return
	 */
	private double compareValues1(double mainVal, double subVal) {
		if (mainVal > subVal) {
			return Constant.EXCELLENT_SCORES;
		} else if (mainVal == subVal) {
			return Constant.EQUAL_SCORES;
		} else {
			return Constant.BAD_SCORES;
		}
	}

	/**
	 * mainVal值越大，得到的分数越低
	 * 
	 * @param mainVal
	 * @param subVal
	 * @return
	 */
	private double compareValues2(double mainVal, double subVal) {
		if (mainVal > subVal) {
			return Constant.BAD_SCORES;
		} else if (mainVal == subVal) {
			return Constant.EQUAL_SCORES;
		} else {
			return Constant.EXCELLENT_SCORES;
		}
	}

	@Override
	public int deleteSameObject(TicketCommodityGrade ticketCommodityGrade) {
		// TODO Auto-generated method stub
		return ticketCommodityGradeMapper.deleteSameObject(ticketCommodityGrade);
	}

}
